package com.jader.fragment;

import java.util.ArrayList;
import java.util.List;

import com.jader.activities.FActivity;
import com.jader.main.MainActivity;
import com.jader.util.JaderApplication;
import com.zlp.jader.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class LunTanFragment extends Fragment {
	View homeView;
	Button button;
	android.support.v4.view.ViewPager viewPager;
	List<Fragment> fragments = new ArrayList<Fragment>();
	RadioGroup radioGroup;
	MyAdapter adapter;
	MainActivity activity;
	ImageButton fabuImagebButton;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		this.activity = (MainActivity) activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		initData();
	}

	private void initData() {

		// 初始化fragments，添加所有、估价、定制、话题四个碎片
		Fragment allFragment = new LunTan_AllFragment();
		Fragment authenticateFragment = new LunTan_AuthenticateFragment();
		Fragment evaluateFragment = new LunTan_EvaluateFragment();
		Fragment topicFragment = new LunTan_TopicFragment();
		fragments.add(allFragment);
		fragments.add(authenticateFragment);
		fragments.add(evaluateFragment);
		fragments.add(topicFragment);

		// 不能传入getFragmentManager(),建议使用getChildFragmentManager，否则
		// 后台转前台，报错。
		adapter = new MyAdapter(getChildFragmentManager());

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		homeView = inflater.inflate(R.layout.luntan, null);
		initViews();
		fabuImagebButton = (ImageButton) homeView
				.findViewById(R.id.fabu_jiahao);
		fabuImagebButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (JaderApplication.user==null) {
					JaderApplication.login(getActivity());
				}else{
				Intent intent = new Intent(getActivity(), FActivity.class);
				startActivity(intent);}
			}
		});
		return homeView;
	}

	private void initViews() {
		viewPager = (ViewPager) homeView.findViewById(R.id.luntanviewpager);
		viewPager.setAdapter(adapter);

		// 监听viewpager状态改变事件
		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					// 说明viewpager滑动到了推荐页面，同步修改推荐选项被选中
					radioGroup.check(R.id.suoyou);// 参数是当前被选中radiobutton的id
					break;
				case 1:
					radioGroup.check(R.id.gujia);
					// 说明viewpager滑动到了故事页面
					break;
				case 2:
					// 说明viewpager滑动到了风景页面
					radioGroup.check(R.id.jianding);
					break;
				case 3:
					// 说明viewpager滑动到了人文页面
					radioGroup.check(R.id.huati);
					break;

				default:
					break;
				}

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});
		// 初始化顶部的单选按钮组
		radioGroup = (RadioGroup) homeView.findViewById(R.id.topber);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			// 同步修改viewpager对应的值
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.suoyou:
					// viewpager指向推荐界面
					viewPager.setCurrentItem(0);
					break;
				case R.id.gujia:
					// viewpager指向故事界面
					viewPager.setCurrentItem(1);
					break;
				case R.id.jianding:
					viewPager.setCurrentItem(2);
					// viewpager指向风景界面
					break;
				case R.id.huati:
					viewPager.setCurrentItem(3);
					// viewpager指向人文界面
					break;

				default:
					break;
				}

			}
		});
	}

	// 写一个适配器，继承FragmentPagerAdapter
	class MyAdapter extends FragmentPagerAdapter {

		public MyAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return fragments.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return fragments.size();
		}

	}
}
